//
//  SHCoreData.m
//  SimpleHappy
//
//  Created by Vincen on 15/11/26.
//  Copyright © 2015年 RockyFung. All rights reserved.
//

#import "SHCoreData.h"

@implementation SHCoreData

// 创建coreData
+ (NSManagedObjectContext *)sHCoreData
{
    static NSManagedObjectContext *context = nil;
    if (context) {
        return context;
    }
    context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"SHCoreData" ofType:@"momd"];
    NSManagedObjectModel *model = [[NSManagedObjectModel alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path]];
    NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    NSString *sqlite = [doc stringByAppendingPathComponent:@"SHCoreData.sqlite"];
    NSLog(@"%@", sqlite);
    
    [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlite] options:nil error:nil];
    context.persistentStoreCoordinator = store;

    return context;
}

// 增
+ (void)addModel:(SHDetailModel *)model
{
    if (model.openUrl != nil && model.picUrl != nil && model.title != nil) {
        NSManagedObjectContext *context = [self sHCoreData];
        WishList *sHmodel = [NSEntityDescription insertNewObjectForEntityForName:@"WishList" inManagedObjectContext:context];
        sHmodel.openUrl = model.openUrl;
        sHmodel.picUrl = model.picUrl;
        sHmodel.title = model.title;
        [context save:nil];
    }
}

// 删 (根据title)
+ (void)deleteModel:(NSString *)title
{
    NSManagedObjectContext *context = [self sHCoreData];
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"WishList"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title = %@", title];
    request.predicate = predicate;
    WishList *sHmodel = [context executeFetchRequest:request error:nil].firstObject;
    if (sHmodel != nil) {
        [context deleteObject:sHmodel];
        [context save:nil];
    }
}

// 查
+ (NSArray *)findAll
{
    NSManagedObjectContext *context = [self sHCoreData];
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"WishList"];
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
    request.sortDescriptors = @[sort];
    NSArray *array = [context executeFetchRequest:request error:nil];
    return  array == nil ? nil : array;
}

// 查标题
+ (NSString *)findWithTitle:(NSString *)title
{
    NSManagedObjectContext *context = [self sHCoreData];
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"WishList"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title = %@", title];
    request.predicate = predicate;
    SHDetailModel *model = [context executeFetchRequest:request error:nil].firstObject;
    return model.title;
}



// 登录状态 增
+ (void)addLogin:(SHMyLoginModel *)isLogin
{
    NSManagedObjectContext *context = [self sHCoreData];
    IsLogin *sHmodel = [NSEntityDescription insertNewObjectForEntityForName:@"IsLogin" inManagedObjectContext:context];
    sHmodel.isLogin = isLogin.isLogin;
}

// 登录状态 查
+ (SHMyLoginModel *)islogin:(NSNumber *)islogin
{
    NSManagedObjectContext *context = [self sHCoreData];
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"IsLogin"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isLogin = %@", islogin];
    request.predicate = predicate;
    SHMyLoginModel *model = [context executeFetchRequest:request error:nil].firstObject;
    return model;
    
}


@end
